package com.changli01.file.exer3;

import org.junit.Test;

import java.io.File;

public class Exer3 {
    //练习
    public static void printFileName(File file){
        if(file.isFile()){
            System.out.println(file.getName());
        }
        else if(file.isDirectory()){
            File [] files = file.listFiles();
            for(File f : files){
                printFileName(f);
            }
        }

    }
    @Test
    public void test() {
        File file = new File("D:\\后端\\Coding\\WorkSpace\\javase\\module15-File_iostream\\src");
        printFileName(file);

    }
    //拓展1
    public static void deleteDirectory(File file){
        if(file.isFile()){
            file.delete();
        }
        else if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                deleteDirectory(f);
            }
        }
    }
    @Test
    public void test2() {
        File file = new File ("C:\\Users\\钰玟\\Desktop\\11");
        deleteDirectory(file);
        file.delete();


    }
//    拓展2
    public long getDirectorySize(File file) {
        long size = 0;
        if(file.isFile()){
            size += file.length();
        }
        else if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f : files){
                size += getDirectorySize(f);
            }
        }
        return size;
    }
    @Test
    public void test3() {
        File file = new File ("D:\\后端\\Coding");
        System.out.println((double)getDirectorySize(file)/(1024*1024));
        System.out.println((double)getDirectorySize(file));
    }
}
